//
//  GameViewController.m
//  RocketAnimation
//
//  Created by iD Student on 7/10/13.
//  Copyright (c) 2013 iD Student. All rights reserved.
//

#import "GameViewController.h"

@interface GameViewController ()

@end

@implementation GameViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    windowHeight = self.view.frame.size.height;
	// Do any additional setup after loading the view.
    imageViewRocket = [[UIImageView alloc]initWithFrame:CGRectMake(133, (4.0/5.0)*windowHeight , 54, 79)];
    imageViewRocket.image = [UIImage imageNamed:@"rocket.png"];
    [self.view addSubview:imageViewRocket];
    rocketYIncrease = 5;
    particles = [[NSMutableArray alloc] init];
    rocketCurrentLocation = CGPointMake(imageViewRocket.frame.origin.x - 3, imageViewRocket.frame.origin.y + 13);
    rocketPreviousLocation = CGPointMake(imageViewRocket.frame.origin.x - 3, imageViewRocket.frame.origin.y + 14);
    background.hidden = true;
    timer1 = [NSTimer scheduledTimerWithTimeInterval:.001 target:self selector:@selector(createParticle) userInfo:nil repeats:YES];
    timer2 = [NSTimer scheduledTimerWithTimeInterval:.033 target:self selector:@selector(updateParticle) userInfo:nil repeats:YES];
    NSLog([NSString stringWithFormat:@"%i", windowHeight]);
    NSLog([NSString stringWithFormat:@"%f", imageViewRocket.frame.origin.y]);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) setRocketPreviousLocation
{
    rocketPreviousLocation = CGPointMake(imageViewRocket.center.x - 5, imageViewRocket.center.y + 16);
}



- (void) newWallpaper
{
    /*
    int i = 0;
    while (i < particles.count) {
        Particle *p = [particles objectAtIndex:i];
        [p.image removeFromSuperview];
    }
     */
}

- (void) createParticle
{
    rocketCurrentLocation = CGPointMake(imageViewRocket.center.x - 5, imageViewRocket.center.y + 13);
    int particlesToSpawn = 5;
    int maxVelocityX = 15;
    int maxLifetime = 2;
    
    for (int i = 0; i < particlesToSpawn; i++) {
        Particle *particle = [[Particle alloc]initWithPoint:rocketCurrentLocation];
        particle.velocityX = arc4random() % maxVelocityX - (.5 * maxVelocityX);
        particle.velocityY = 15; //(rocketCurrentLocation.y - rocketPreviousLocation.y);
        particle.lifetime = arc4random() % maxLifetime + 2;
        [particles addObject:particle];
        [self.view addSubview:particle.image];
        [self.view sendSubviewToBack:particle.image];
        [self.view sendSubviewToBack:background];
        
    }
}

- ()

- (void) updateRocket
{
    if (imageViewRocket.center.y <= (-1)*imageViewRocket.frame.size.height/2+1) {
        
        imageViewRocket.center = CGPointMake(imageViewRocket.center.x, self.view.frame.size.height + 1 + (imageViewRocket.frame.size.height/2));
        if (rocketYIncrease <= 30)
        {
            [imageViewRocket setCenter:CGPointMake(imageViewRocket.center.x, imageViewRocket.center.y - rocketYIncrease)];
            //rocketYIncrease += 1;
        } else {
            [imageViewRocket setCenter:CGPointMake(imageViewRocket.center.x, imageViewRocket.center.y - rocketYIncrease)];
        }
        [self newWallpaper];
        
    }
    else {
        if (rocketYIncrease <= 30)
        {
            [imageViewRocket setCenter:CGPointMake(imageViewRocket.center.x, imageViewRocket.center.y - rocketYIncrease)];
            //rocketYIncrease += 1;
        } else {
            [imageViewRocket setCenter:CGPointMake(imageViewRocket.center.x, imageViewRocket.center.y - rocketYIncrease)];
        }
    }
}

- (void) updateParticle
{
    int i = 0;
    while (i < particles.count) {
        Particle *p = [particles objectAtIndex:i];
        if ([p isAlive])
        {
            [p update];
            i++;
        } else {
            [p.image removeFromSuperview];
            [particles removeObject:p];
        }
    }
}

- (IBAction)changeRocketAnimation:(id)sender
{
        timer3 = [NSTimer scheduledTimerWithTimeInterval:.033 target:self selector:@selector(setRocketPreviousLocation) userInfo:nil repeats:YES];
        sleep(.0165);
        timer4 = [NSTimer scheduledTimerWithTimeInterval:.033 target:self selector:@selector(updateRocket) userInfo:nil repeats:YES];
    buttonChangeAnimation.hidden = true;
}



@end
